home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8206 / 8206.xpi / content / editor.js < prev    next >
Text File  |  2010-02-02  |  9KB  |  312 lines

  1. /*
  2.  Some of the editor code borrowed from mozilla's midas demo:
  3.  http://www.mozilla.org/editor/midasdemo/
  4. */
  5.  
  6. var WiseStampEditors = 
  7. {
  8.     current: null,
  9.     _doc: null,
  10.     get doc() {
  11.         return this._doc;
  12.         //return this.current.contentWindow.document;
  13.     },
  14.  
  15.     doCommand: function (obj) 
  16.     {
  17.         aType = obj.getAttribute('cmdtype');
  18.  
  19.         if ((aType == "fontsize") || (aType == "fontname")) {
  20.             this.command = aType;
  21.         }
  22.         if ((aType == "forecolor") || (aType == "hilitecolor")) {
  23.             this.command = aType;
  24.         }
  25.         if ((aType == "justifyleft") || (aType == "justifycenter") || (aType == "justifyright")) {
  26.  
  27.             // need to check the current state according to the cursor position in the rich editor
  28.             //document.getElementById("justifyleft").className = 'unpressed';
  29.             //document.getElementById("justifycenter").className = 'unpressed';
  30.             //document.getElementById("justifyright").className = 'unpressed';
  31.         }
  32.         if (aType == "createlink") {
  33.     
  34.             var link = WiseStampUtils.prompt("createlinkTitle", "http://", "createlink");
  35.             if (link == null) 
  36.                 return;
  37.  
  38.             if (link != "") 
  39.                 this.doc.execCommand("CreateLink", false, link);
  40.             else 
  41.                 WiseStampUtils.alert("invalid_link_error");
  42.         
  43.         } else if (aType == "createimage") 
  44.         {
  45.             imagePath = WiseStampUtils.prompt("createimageTitle", "http://", "createimage");
  46.             if (imagePath == null) 
  47.                 return;
  48.  
  49.             if (imagePath != "")
  50.             {
  51.                 var html = '<img border=0 src='+imagePath+' />';
  52.                 this.doc.execCommand('InsertHTML',false,html);
  53.             }
  54.             else 
  55.                 WiseStampUtils.alert("invalid_image_error");
  56.         
  57.         } else if (aType == "createtable") 
  58.         {
  59.             e = this.current;
  60.             rowstext = prompt(WisestampSignatureFactory.strings.GetStringFromName("createtablerows"));
  61.             colstext = prompt(WisestampSignatureFactory.strings.GetStringFromName("createtablecolumns"));
  62.             rows = parseInt(rowstext);
  63.             cols = parseInt(colstext);
  64.             if ((rows > 0) && (cols > 0)) 
  65.             {
  66.                 table = this.doc.createElement("table");
  67.                 table.setAttribute("border", "1");
  68.                 table.setAttribute("cellpadding", "2");
  69.                 table.setAttribute("cellspacing", "2");
  70.                 tbody = this.doc.createElement("tbody");
  71.                 for (var i = 0; i < rows; i++) 
  72.                 {
  73.                     tr = this.doc.createElement("tr");
  74.                     for (var j = 0; j < cols; j++) 
  75.                     {
  76.                         td = this.doc.createElement("td");
  77.                         br = this.doc.createElement("br");
  78.                         td.appendChild(br);
  79.                         tr.appendChild(td);
  80.                     }
  81.  
  82.                     tbody.appendChild(tr);
  83.                 }
  84.                 table.appendChild(tbody);
  85.                 this._insertNodeAtSelection(e.contentWindow, table);
  86.             }
  87.  
  88.         } else if (aType == "ltr" || aType == "rtl") 
  89.         {
  90.             this.selectWritingDirection(aType);
  91.         
  92.         } else 
  93.         {
  94.             this.doc.execCommand(aType, false, null);
  95.         }
  96.  
  97.         if (obj.className == 'pressed') 
  98.             obj.className = 'unpressed';
  99.         else if (obj.className == 'unpressed') 
  100.             obj.className = 'pressed';
  101.     },
  102.     
  103.     _insertNodeAtSelection: function (win, insertNode) 
  104.     {
  105.         var sel = win.getSelection();
  106.         var range = sel.getRangeAt(0);
  107.         sel.removeAllRanges();
  108.         range.deleteContents();
  109.         var container = range.startContainer;
  110.         var pos = range.startOffset;
  111.         range = document.createRange();
  112.  
  113.         if (container.nodeType == 3 && insertNode.nodeType == 3) 
  114.         {
  115.             container.insertData(pos, insertNode.nodeValue);
  116.             // put cursor after inserted text
  117.             range.setEnd(container, pos + insertNode.length);
  118.             range.setStart(container, pos + insertNode.length);
  119.         
  120.         } else 
  121.         {
  122.             var afterNode;
  123.             if (container.nodeType == 3) 
  124.             {
  125.                 var textNode = container;
  126.                 container = textNode.parentNode;
  127.                 var text = textNode.nodeValue;
  128.  
  129.                 // text before the split
  130.                 var textBefore = text.substr(0, pos);
  131.                 // text after the split
  132.                 var textAfter = text.substr(pos);
  133.  
  134.                 var beforeNode = document.createTextNode(textBefore);
  135.                 afterNode = document.createTextNode(textAfter);
  136.  
  137.                 // insert the 3 new nodes before the old one
  138.                 container.insertBefore(afterNode, textNode);
  139.                 container.insertBefore(insertNode, afterNode);
  140.                 container.insertBefore(beforeNode, insertNode);
  141.  
  142.                 // remove the old node
  143.                 container.removeChild(textNode);
  144.             
  145.             } else 
  146.             {
  147.                 // else simply insert the node
  148.                 afterNode = container.childNodes[pos];
  149.                 container.insertBefore(insertNode, afterNode);
  150.             }
  151.  
  152.             range.setEnd(afterNode, 0);
  153.             range.setStart(afterNode, 0);
  154.         }
  155.  
  156.         sel.addRange(range);
  157.     },
  158.     
  159.     colorPickerShowing: function (event) 
  160.     {
  161.         this.command = document.popupNode.getAttribute("cmdtype");
  162.         return true;
  163.     },
  164.     
  165.     colorPickerHiding: function () 
  166.     {
  167.         this.current.focus();
  168.     },
  169.     
  170.     colorPickerClick: function (event) 
  171.     {
  172.         var target = event.originalTarget;
  173.         if (target.tagName == "box") {
  174.             var color = target.id;
  175.             document.getElementById("colorpicker").hidePopup();
  176.             this.doc.execCommand(this.command, false, color);
  177.             this.current.contentWindow.focus();
  178.             this.command = null;
  179.         }
  180.     },
  181.     
  182.     changeFont: function (event, mode) {
  183.         var value = event.target.getAttribute("value");
  184.         this.doc.execCommand("font" + mode, false, value);
  185.     },
  186.     
  187.     showFontName: function (event) {
  188.         var mixed = {}
  189.         var font = this.editor.getFontFaceState(mixed);
  190.         var ele = this.current.contentWindow.getSelection().anchorNode;
  191.         if (ele) {
  192.             ele = ele.parentNode;
  193.             var style = this.current.contentWindow.getComputedStyle(ele, "");
  194.             var reg = /['"](.+)['"]\s*,\s*.+/
  195.             var fam = style.fontFamily.replace(reg, "$1").toLowerCase();
  196.             var found = false;
  197.             Array.forEach(event.target.childNodes, function (node) {
  198.                 if (fam == node.value.replace(reg, "$1").toLowerCase()) {
  199.                     node.setAttribute("checked", "true");
  200.                     found = true;
  201.         } else
  202.           node.removeAttribute("checked");
  203.             });
  204.         }
  205.     },
  206.     
  207.     selectWritingDirection: function (direction) 
  208.     {
  209.         if (direction == "none") {
  210.             //this.removeTextProperty("*","dir");
  211.         } else {
  212.             this.current.contentDocument.dir = direction;
  213.             //this.setTextProperty("*", "dir", direction);
  214.         }
  215.     },
  216.     
  217.     get editor() {
  218.         try {
  219.             Ci = Components.interfaces;
  220.             Cc = Components.classes;
  221.             var win = this.current.contentWindow;
  222.             var editingSession = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIEditingSession);
  223.             if (editingSession.windowIsEditable(win)) {
  224.                 var editor = editingSession.getEditorForWindow(win);
  225.                 return editor.QueryInterface(Components.interfaces.nsIHTMLEditor);
  226.             }
  227.         }
  228.         catch(ex) {}
  229.         return null;
  230.     },
  231.     
  232.     get editorForWin() {
  233.         try {
  234.             Ci = Components.interfaces;
  235.             Cc = Components.classes;
  236.             var win = this.current.contentWindow;
  237.             var editingSession = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIEditingSession);
  238.             if (editingSession.windowIsEditable(win)) {
  239.                 var editor = editingSession.getEditorForWindow(win);
  240.                 return editor;
  241.             }
  242.         }
  243.         catch(ex) {}
  244.         return null;
  245.     },
  246.     
  247.     removeTextProperty: function (property, attribute) 
  248.     {
  249.         {
  250.             var atomService = Components.classes["@mozilla.org/atom-service;1"].
  251.             getService(Components.interfaces.nsIAtomService);
  252.             var propAtom = atomService.getAtom(property);
  253.  
  254.             this.editor.removeInlineProperty(propAtom, attribute);
  255.         }
  256.     },
  257.     
  258.     setTextProperty: function (property, attribute, value) 
  259.     {
  260.         {
  261.             var atomService = Components.classes["@mozilla.org/atom-service;1"].
  262.             getService(Components.interfaces.nsIAtomService);
  263.             var propAtom = atomService.getAtom(property);
  264.             this.editor.setInlineProperty(propAtom, attribute, value);
  265.         }
  266.     }
  267.     
  268. }
  269.  
  270. function WiseStampInitWYSIWYGEditor(editor, html, dir)
  271. {
  272.     WiseStampUtils.log("[editor.js::WiseStampInitWYSIWYGEditor] html = " + html);
  273.     
  274.     WiseStampEditors.current = editor;
  275.  
  276.     var eles = document.getElementById("editor-toolbar-simple").childNodes;
  277.     for (var i = 0; i < eles.length; i++)
  278.     {
  279.         var cmdtype = eles[i].getAttribute("cmdtype");
  280.         if (cmdtype)
  281.         {
  282.             eles[i].setAttribute("src", "chrome://wisestamp/skin/editor/" + cmdtype + ".gif");
  283.             eles[i].setAttribute("onclick", "WiseStampEditors.doCommand(event.target)");
  284.         }
  285.     }
  286.  
  287.     if (editor.hasAttribute("initialized"))
  288.     {
  289.         editor.contentDocument.body.innerHTML = html;
  290.         editor.contentDocument.dir = dir;
  291.         editor.contentDocument.designMode = 'on';
  292.         WiseStampEditors._doc = editor.contentDocument;
  293.     }
  294.     else
  295.     {
  296.         WiseStampEditors._doc = editor.contentDocument;
  297.         
  298.         function initEditor()
  299.         {
  300.             WiseStampUtils.log("[editor.js::WiseStampInitWYSIWYGEditor] onLoad - initEditor");
  301.             editor.contentDocument.body.innerHTML = html;
  302.             editor.contentDocument.dir = dir;
  303.             editor.contentDocument.designMode = 'on';
  304.             WiseStampEditors._doc = editor.contentDocument;
  305.             
  306.             editor.setAttribute("initialized","true");            
  307.         }
  308.         
  309.         setTimeout(initEditor,0); // event is not fired on Thunderbird - this works though
  310.         editor.contentWindow.addEventListener("load",initEditor,false);
  311.     }
  312. }